

$(document).ready(function(){

    //show surcharge button
    $(".list-item-action-show-details .glyphicon-menu-up").hide();
    $(".list-item-rate").hide();
    $(".list-item-action-show-details").bind("click",function(){
        if($(this).find(".glyphicon-menu-up").is(":hidden")){
            $(this).find(".glyphicon-menu-up").show();
            $(this).find(".glyphicon-menu-down").hide();
            $(this).parent().parent().find(".list-item-rate").show();
            //$(this).removeClass("subGroupBottomBorder");
        }else{
            $(this).find(".glyphicon-menu-down").show();
            $(this).find(".glyphicon-menu-up").hide();
            $(this).parent().parent().find(".list-item-rate").hide();
            // $(this).addClass("subGroupBottomBorder");

        }
    });

    $("#btnFindSurcharge").click(function () {
        var objlstItem = $(this).parentsUntil("li");
        if($(objlstItem).find(".list-item-action-show-details .glyphicon-menu-up").is(":hidden")){
            $(objlstItem).find(".list-item-action-show-details .glyphicon-menu-up").show();
            $(objlstItem).find(".list-item-action-show-details .glyphicon-menu-down").hide();
            $(objlstItem).parent().find(".list-item-rate").show();
        }
    })

    $(".list-item-rate .glyphicon-menu-down").hide();
    $(".list-item-rate-subgroup").addClass("list-item-rate-subgroup-highlight");
    $(".oceanFreight").bind("click",function(){
        if($(this).find(".glyphicon-menu-up").is(":hidden")){
            $(this).find(".glyphicon-menu-up").show();
            $(this).find(".glyphicon-menu-down").hide();
            $(this).parent().find(".list-item-rate-ocean-freight").show();
            $(this).parent().find(".oceanFreight").addClass("list-item-rate-subgroup-highlight");
            //$(this).removeClass("subGroupBottomBorder");
        }else{
            $(this).find(".glyphicon-menu-down").show();
            $(this).find(".glyphicon-menu-up").hide();
            $(this).parent().find(".list-item-rate-ocean-freight").hide();
            $(this).parent().find(".oceanFreight").removeClass("list-item-rate-subgroup-highlight");
           // $(this).addClass("subGroupBottomBorder");

        }
    });
    $(".originSurcharge").bind("click",function(){
        if($(this).find(".glyphicon-menu-up").is(":hidden")){
            $(this).find(".glyphicon-menu-up").show();
            $(this).find(".glyphicon-menu-down").hide();
            $(this).parent().find(".list-item-rate-surcharge").show();
            $(this).parent().find(".originSurcharge").addClass("list-item-rate-subgroup-highlight");
           // $(this).removeClass("subGroupBottomBorder");
        }else{
            $(this).find(".glyphicon-menu-down").show();
            $(this).find(".glyphicon-menu-up").hide();
            $(this).parent().find(".list-item-rate-surcharge").hide();
            $(this).parent().find(".originSurcharge").removeClass("list-item-rate-subgroup-highlight");
           // $(this).addClass("subGroupBottomBorder");
        }
    });
    $(".destinationSurcharge").bind("click",function(){
        if($(this).find(".glyphicon-menu-up").is(":hidden")){
            $(this).find(".glyphicon-menu-up").show();
            $(this).find(".glyphicon-menu-down").hide();
            $(this).parent().find(".list-item-rate-destinationSurcharge").show();
            $(this).parent().find(".destinationSurcharge").addClass("list-item-rate-subgroup-highlight");
            // $(this).removeClass("subGroupBottomBorder");
        }else{
            $(this).find(".glyphicon-menu-down").show();
            $(this).find(".glyphicon-menu-up").hide();
            $(this).parent().find(".list-item-rate-destinationSurcharge").hide();
            $(this).parent().find(".destinationSurcharge").removeClass("list-item-rate-subgroup-highlight");
            // $(this).addClass("subGroupBottomBorder");
        }
    });

    $(".btn-enquiry-chat").hide();
    $(".btn-instant-booking").hide();
    $(".my-rate-list li").bind("mouseover",function(){
        $(this).find(".btn-enquiry-chat").show();
        $(this).find(".btn-instant-booking").show();

    }).bind("mouseout",function(){
        $(this).find(".btn-enquiry-chat").hide();
        $(this).find(".btn-instant-booking").hide();
    });

    $(".btn-instant-booking").click(function(){
        var agreement_no = $(this).parent().parent().parent().parent().find(".list-item-agreement-number span").first().text();
        agreement_no = agreement_no.split('.')[1];

        window.open('booking.html?an='+agreement_no);
    })

    $(".list-item-rate .list-item-rate-subgroup").bind("mouseover",function(){
        $(this).addClass("rowHover");
    }).bind("mouseout",function(){
        $(this).removeClass("rowHover");
    });
    //Advanced Search
    $(".search-filters .search-filter-field .search-filter-input").blur(function(){
        var searchKey = $(this).val();
        $(this).val('');
        if(searchKey!=''){
            var filterItem = "<li class='search-filter-item'><span>"+searchKey+"</span><span class='glyphicon glyphicon-remove'></span></li>";
            if(!existFilterItem(searchKey)){
                $(this).parent().before(filterItem);
                bindRemoveEvt();
            }
        }
        $(this).parent().hide();

    });

    $("ul.search-filters").bind("click",function(){
        $(".search-filters .search-filter-field").show();
        $(".search-filters .search-filter-field .search-filter-input").focus();
    });

    $('#etd_date_range').dateRangePicker({
        customTopBar: 'Set ETD Date Range',
        autoClose: true,
        separator: ' - ',
        format: 'DD MMM YYYY',
        language: 'auto'
    });

    $(".oal-datepicker .input-group-addon").bind("click",function(evt){
        evt.stopPropagation();
        $(this).parent().find("input").focus().click();

    });
    var city = [
        {"name":"hong kong"},{"name":"shang hai"}
    ]

    $("#pol").combobox({
        data:city,
        minLength:0,
        match:'name',
        getSelected:function(item){
            var polvalue="";
            if(item.length>0){
                $.each(item,function (i) {
                    if(i>0){
                        polvalue+=','
                    }
                    polvalue += item[i].name;
                })
            }
            $("#hidden_pol").val(polvalue);
        }
    });
    $("#pod").combobox({
        data:city,
        minLength:0,
        match:'name',
        getSelected:function(item){
            var podvalue="";
            if(item.length>0){
                $.each(item,function (i) {
                    if(i>0){
                        podvalue+=', '
                    }
                    podvalue += item[i].name;
                })
            }
            $("#hidden_pod").val(podvalue);

        }
    });

    //Filter
    $('#myModal').on('hidden.bs.modal', function (e) {
        var etd= $('#etd_date_range').val();


        var pol= $('#hidden_pol').val();
        var pod= $('#hidden_pod').val();
        var lst_commodiy= $('#lst_commodiy').val();
        var agreement_number = $('#agreement_number').val();


        if((etd + pol + pod + lst_commodiy + agreement_number ) !='' ){
            $(".search-filters .search-filter-item").each(function(){
                if($(this).text().indexOf(':')>0){
                    $(this).remove();
                }
            });

            var filterfield = $(".search-filters .search-filter-field");

            if(etd !=''){
                var filterItem = "<li class='search-filter-item'><span>ETD: "+etd+"</span><span class='glyphicon glyphicon-remove'></span></li>";
                filterfield.before(filterItem);

            }

            if(pol !=''){
                var filterItem = "<li class='search-filter-item'><span>POR: "+pol+"</span><span class='glyphicon glyphicon-remove'></span></li>";
                filterfield.before(filterItem);
            }

            if(pod !=''){
                var filterItem = "<li class='search-filter-item'><span>FND: "+pod+"</span><span class='glyphicon glyphicon-remove'></span></li>";
                filterfield.before(filterItem);
            }

            if(lst_commodiy !=''){
                var filterItem = "<li class='search-filter-item'><span>Commodity: "+lst_commodiy+"</span><span class='glyphicon glyphicon-remove'></span></li>";
                filterfield.before(filterItem);
            }

            if(agreement_number !=''){
                var filterItem = "<li class='search-filter-item'><span>Agreement Number: "+agreement_number+"</span><span class='glyphicon glyphicon-remove'></span></li>";
                filterfield.before(filterItem);
            }

        }

        bindRemoveEvt();
        $("#searchinput").hide();

        if($(".search-filters .search-filter-item").length > 0){
            $(".search-filters").show();

        }

    });


    $('#btnAdvancedSearch').click(function(){
        $('#myModal').modal('hide');
    });
});

// Search Filter
function existFilterItem(itemText){
    var existFlag = false;
    $(".search-filters .search-filter-item").each(function(){
        if($(this).text() == itemText){
            existFlag = true;
        }
    });
    return existFlag;
}
function bindRemoveEvt(){
    $("ul.search-filters .search-filter-item .glyphicon-remove").click(function(){
        $(this).parent().remove();
    })
};
